use std::collections::HashMap;

use crate::{client::RedisClient, resp::Command, RedisResult};

impl RedisClient {
    pub async fn hash_hset(
        &self,
        key: &str,
        field: impl Into<String>,
        value: impl Into<String>,
    ) -> RedisResult<usize> {
        let cmd = Command::new_with_key("HSET", key)
            .arg(field.into())
            .arg(value.into());
        self.send_command(cmd).await
    }

    pub async fn hash_hgetall(&self, key: &str) -> RedisResult<HashMap<String, String>> {
        let cmd = Command::new_with_key("HGETALL", key);
        self.send_command(cmd).await
    }

    pub async fn hash_hdel(&self, key: &str, field: &str) -> RedisResult<usize> {
        let cmd = Command::new_with_key("HDEL", key).arg(field);
        self.send_command(cmd).await
    }

    pub async fn hash_hdel_more(&self, key: &str, fields: &[&str]) -> RedisResult<usize> {
        let mut cmd = Command::new_with_key("HDEL", key);
        for field in fields {
            cmd.arg_append(*field);
        }
        self.send_command(cmd).await
    }

    pub async fn hash_hexists(&self, key: &str, field: &str) -> RedisResult<bool> {
        let cmd = Command::new_with_key("HEXISTS", key).arg(field);
        let result = self.send_command::<i32>(cmd).await?;
        Ok(result == 1)
    }
}
